package com.caih.contacts.service.impl;

import com.caih.contacts.entity.Department;
import com.caih.contacts.entity.Employees;
import com.caih.contacts.entity.Result;
import com.caih.contacts.service.WeiXinDepartmentService;
import com.caih.contacts.util.AccessToken;
import com.caih.contacts.util.WeiXinRequest;
import com.caih.contacts.util.WeiXinSercret;
import com.google.gson.Gson;
import net.sf.json.JSONObject;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
public class WeiXinDepartmentServiceimpl implements WeiXinDepartmentService {

    private  static  String createDepartment_url="https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token=ACCESS_TOKEN";
    private  static  String updateDepartment_url="https://qyapi.weixin.qq.com/cgi-bin/department/update?access_token=ACCESS_TOKEN";
    private  static  String deleteDepartment_url="https://qyapi.weixin.qq.com/cgi-bin/department/delete?access_token=ACCESS_TOKEN&id=ID";

    /**
     * 创建部门接口
     * @param accessToken
     * @param department
     * @return
     */
    @Override
    public Result<Department> createDep(String accessToken, Department department) {
        Gson gson = new Gson();
        String jsonDepartment = gson.toJson(department);
        createDepartment_url=createDepartment_url.replace("ACCESS_TOKEN", accessToken);
        JSONObject jsonObject = WeiXinRequest.httpsRequest(createDepartment_url, "POST", jsonDepartment.replace("_",""));
        Result<Department> departmentResult = new Result<>();
        departmentResult.setCode(jsonObject.getInt("errcode"));
        departmentResult.setMessage(jsonObject.getString("errmsg"));
        departmentResult.setData(department);
        return departmentResult;
    }
    /**
     * 更新部门列表
     * @param accessToken
     * @param department
     * @return
     */
    @Override
    public Result<Department> updateDep(String accessToken, Department department) {
        Gson gson=new Gson();
        String jsonui=gson.toJson(department);
        System.out.println("jsonui"+jsonui);
        updateDepartment_url = updateDepartment_url.replace("ACCESS_TOKEN",accessToken);
        JSONObject jsonObject= WeiXinRequest.httpsRequest(updateDepartment_url,"POST",jsonui);
        //System.out.println("jsonobject"+jsonObject.toString());
        Result<Department> result = new Result<>();

        if(jsonObject!=null){
            if(jsonObject.getInt("errcode")!=0){
                //  log.error("更新成员失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));
                result.setCode(jsonObject.getInt("errcode"));
                result.setMessage(jsonObject.getString("errmsg"));
            }
            else{
                result.setCode(jsonObject.getInt("errcode"));
                result.setMessage(jsonObject.getString("errmsg"));
                result.setData(department);
            }
        }
        return result;


    }

    /**
     * @param accessToken
     * @param id
     * 删除部门列表
     * @return
     */
    @Override
    public Result<String> deleteDep(String accessToken, Integer id) {
        //1.获取请求的url
        deleteDepartment_url = deleteDepartment_url.replace("ACCESS_TOKEN", accessToken).replace("ID",id.toString());
        //2.调用接口，发送请求，删除列表
        JSONObject jsonObject = WeiXinRequest.httpsRequest( deleteDepartment_url, "GET", null);
        System.out.println("jsonObject:"+jsonObject.toString());
           Result<String> result= new Result<>();
        // 如果请求成功
        result.setCode(jsonObject.getInt("errcode"));
        result.setMessage(jsonObject.getString("errmsg"));
        if (jsonObject.getInt("errcode")==0){
            result.setData("删除成功");
        }else{
            result.setData("删除失败");
        }
        return result;
    }

    /**
     * 获取Access_Token保存Cache
     * @return
     */
    @Override
    @Cacheable(value = "accessToken")
    public  AccessToken getAccessToken() {
        System.out.println("调用此方法");
        return WeiXinRequest.getAccessToken(WeiXinSercret.corpId,WeiXinSercret.corpSecret);
    }

    /**
     * 刷新Cache
     * @return
     */
    @Override
    @Scheduled(fixedRate = 3600000)
    @CachePut(value = "accessToken")
    public AccessToken updateAccessToken() {
        System.out.println("更新方法");
        return WeiXinRequest.getAccessToken(WeiXinSercret.corpId,WeiXinSercret.corpSecret);
    }

}
